define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'xilumarket/user/user/index' + location.search,
                    add_url: 'xilumarket/user/user/add',
                    edit_url: 'xilumarket/user/user/edit',
                    del_url: 'xilumarket/user/user/del',
                    multi_url: 'xilumarket/user/user/multi',
                    table: 'xilumarket_user_auth',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                escape:false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'avatar', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'gender', title: __('Gender'), visible: false, searchList: {1: __('Male'), 0: __('Female')}},
                        {field: 'successions', title: __('Successions'), visible: false, operate: 'BETWEEN', sortable: true},
                        {field: 'money', title: __('Money'), operate: false,sortable: true},
                        {field: 'auth.check_status', title: __('实名认证'),formatter:function(val,row,index){
                              var  a = !row.auth || !row.auth.id ?"待实名":'';
                                if(row.auth && row.auth.id){
                                    if(row.auth.check_status==0){
                                         a = '<a href="'+Fast.api.fixurl('xilumarket/user/user/personal/ids/'+row.id)+'" class="btn btn-primary btn-xs btn-dialog" data-title="资料">待审核</a>';
                                    }else if(row.auth.check_status==1){
                                         a = '<a href="'+Fast.api.fixurl('xilumarket/user/user/personal/ids/'+row.id)+'" class="btn btn-success btn-xs btn-dialog" data-title="资料">已审核</a>';
                                    }else if(row.auth.check_status==2){
                                         a = '<a href="'+Fast.api.fixurl('xilumarket/user/user/personal/ids/'+row.id)+'" class="btn btn-danger btn-xs btn-dialog" data-title="资料">已拒绝</a>';
                                    }
                                }
                                return a;
                        }},
                        {field: 'logintime', title: __('Logintime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'loginip', title: __('Loginip'), formatter: Table.api.formatter.search},
                        {field: 'jointime', title: __('Jointime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'status', title: __('Status'), formatter: Table.api.formatter.status, searchList: {normal: __('Normal'), hidden: __('Hidden')}},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });
            //当表格数据加载完成时
            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                console.log(data);
                //这里我们手动设置底部的值
                $("#check-users").text(data.extend.users);
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        personal: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
